var admins = [];
var currentPage = 1;
var totalPage = 1;
// 查询所有管理员信息
function queryAllAdmins() {
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token
	};
	$
			.ajax({
				type : "POST",
				url : "/ThreeNewBoard/queryalladminuser",
				data : JSON.stringify(options),
				crossDomain : true,
				async : true,
				contentType : "application/json",
				dataType : "json",
				success : function(result) {
					if (result.message == "SUCCESS") {
						// data为请求到的数据
						var data = result.data;
						users = data;
						totalPage = Math.ceil(data.length / 10);
						if (totalPage === 0) {
							var noData = "<div style='width:100%;height:30px;margin:40px 0;text-align: center;'>暂无表格数据</div>"
							document.getElementById("adminListNoData").innerHTML = noData;
						}
						document.getElementById("adminTotal").innerText = "";
						$("#adminTotal").append(totalPage);
						dealData();
					} else {
						alert("数据请求错误！");
					}
				}
			});

}

// 处理表格数据展示
function dealData() {
	var displayData = [];
	$("#adminList").find("tr").remove();
	if (totalPage < 1 || currentPage === totalPage) {
		$("#nextPageBtn").attr("disabled", true);
	} else {
		$("#nextPageBtn").attr("disabled", false);
	}
	if (currentPage === 1) {
		$("#prePageBtn").attr("disabled", true);
	} else {
		$("#prePageBtn").attr("disabled", false);
	}
	document.getElementById("currentPage").innerText = currentPage;
	for (var i = (currentPage - 1) * 10; i < currentPage * 10; i++) {
		var item = users[i];
		if (!item) {
			return;
		}
		displayData.push(item);
		row = document.createElement("tr"); // 创建行
		td1 = document.createElement("td"); // 姓名
		td2 = document.createElement("td"); // 账号
		td3 = document.createElement("td"); // 手机号
		td1.appendChild(document.createTextNode(item.userName));
		td2.appendChild(document.createTextNode(item.accountNumber));
		td3.appendChild(document.createTextNode(item.phone));
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		$("#adminList").append(row);
	}
}
// 上一页
function prePage() {
	currentPage--;
	dealData();
}
// 下一页
function nextPage() {
	currentPage++;
	dealData();
}
// 提交时校验参数
function validParam() {
	var name = document.getElementById("admName").value;
	if (name === "") {
		alert("姓名不能为空。");
		return;
	}
	var phone = document.getElementById("admPhone").value;
	if (phone === "") {
		alert("手机号不能为空。");
		return;
	}
	var passWord = document.getElementById("admPsd").value;
	if (passWord === "") {
		alert("密码不能为空。");
		return;
	}
	var nameError = document.getElementById("errorAdmName").innerHTML;
	if (nameError) {
		alert("姓名输入不合法，请确认。");
		return;
	}
	var phoneError = document.getElementById("errorAdmPhone").innerHTML;
	if (phoneError) {
		alert("手机号输入不合法，请确认。");
		return;
	}
	var psdError = document.getElementById("errorAdmPsd").innerHTML;
	if (psdError) {
		alert("密码输入不合法，请确认。");
		return;
	}
	var psdAgain= document.getElementById("admAgainPsd").value;
	if (psdAgain !== passWord) {
		alert("两次密码输入不一致，请确认。");
		return;
	}
	createAdmin();
}
// 创建管理员
function createAdmin() {
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"phone" : document.getElementById("admPhone").value,
		"passWord" : document.getElementById("admPsd").value,
		"userName" : document.getElementById("admName").value
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/adminregister",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			closeWindow('createAdminModal');
			var data = result.data;
			if (data.isRegister) {
				// data为请求到的数据
				alert("注册成功，请使用" + data.accountNumber + "登陆");
				queryAllAdmins();
			} else {
				alert("数据请求错误！");
			}
		}
	});
}
// 校验名称
function validAdmName(value, id) {
	document.getElementById(id).innerText = "";
	if (!userNameValidator.test(value)) {
		document.getElementById(id).innerText = "请输入合法的姓名。由中文字符、英文字母、中划线（-）、下划线（_）、点（·）组成，长度是1到255位。";
		return false;
	}
}
// 校验密码
function validAdmPsd(value, id) {
	document.getElementById(id).innerText = "";
	if (!passwordValidator.test(value)) {
		document.getElementById(id).innerText = "密码以字母开头，长度在6-18之间，只能包含英文字母、数字和下划线。";
		return false;
	}
}
// 确认密码
function againAdmPsd(value, id) {
	document.getElementById(id).innerText = "";
	var psd = document.getElementById("admPsd").value;
	if (value !== psd) {
		document.getElementById(id).innerText = "两次输入密码不一致。";
		return false;
	}
}